export default class Timer {
  constructor (params) {
    this.duration = params.duration || 60
    this.timerId = undefined
    this.startTips = params.tips || '获取验证码'
    this.runingTips = params.runingTips || '倒计时'
    this.status = this.startTips
    this.onStart = params.onStart
    this.onStop = params.onStop
    this.onRunning = params.onRunning
    this.start()
  }

  start () {
    this.timerId && clearInterval(this.timerId)
    this.timerId = setInterval(() => {
      this.onStart && this.onStart(this)
      this.onRunning && this.onRunning(this)
      if (this.duration < 1) {
        this.stop()
      }
      this.status = `${this.runingTips}（${this.duration}）`
      this.duration--
    }, 1000)
  }
  stop () {
    this.timerId && clearInterval(this.timerId)
    this.status = this.startTips
    this.onStop && this.onStop(this)
    this.onRunning && this.onRunning(this)
  }
}
